var ibMarketItemForm = {
	/*------------------------------*/
	/* Constructor 					*/
	init: function()
	{
		Debug.write("Initializing itemForm.js");
		
		document.observe("dom:loaded", function(){
			ibMarketItemForm.loadObserve();
			ibMarketItemForm.updateRestock();
		});
	},
	loadObserve: function()
	{
		$('item[stock]').observe('keyup', this.changeValue);
		
		$('item[restock_howmuch]').observe('keyup', this.changeValue);
		
		$('item[restock]').observe('change', this.changeValue);
		$('item[restock]').observe('keyup', this.changeValue);
		
		$('item[restock_when]').observe('keyup', this.changeValue);
		
		$('item[restock_minute]').observe('change', this.changeValue);
		$('item[restock_minute]').observe('keyup', this.changeValue);
		$('item[restock_hour]').observe('change', this.changeValue);
		$('item[restock_hour]').observe('keyup', this.changeValue);
		$('item[restock_day]').observe('change', this.changeValue);
		$('item[restock_day]').observe('keyup', this.changeValue);
		
		$('restockButton').observe('click', this.setTime);
	},
	changeValue: function(event)
	{
		ibMarketItemForm.updateRestock();
	},
	updateRestock: function()
	{
		Debug.write("Updating itemForm for restock");
		
		var form = $('itemForm');
		var howMuch = parseInt($F($('item[restock_howmuch]')));
		var stock = $F($('item[stock]')) == 'infinite' ? -1 : parseInt($F($('item[stock]')));
		stock = isNaN(stock) ? 0 : stock;
		
		if(isNaN(howMuch) || howMuch <= 0 || $F($('item[restock]')) == 0 || isNaN(stock) || stock < 0)
		{
			$('item[restock_when]').disable();
			$('item[restock_minute]').disable();
			$('item[restock_hour]').disable();
			$('item[restock_day]').disable();
			$('restockButton').disable();
			$('restockButton').hide();
			$('li_restock_when').hide();
			$('li_restock_time').hide();
			
			$('restock_when_ques').innerHTML = "";
			$('restock_when_desc').innerHTML = "";
			$('restock_time_ques').innerHTML = "";
			$('restock_time_desc').innerHTML = "";
			
			$('restockInfo').value = ipb.lang['restock_no_restock'];
			return;
		}
		else if($F($('item[restock]')) == 1)
		{
			$('item[restock_when]').enable();
			$('item[restock_minute]').enable();
			$('item[restock_hour]').enable();
			$('item[restock_day]').enable();
			$('restockButton').disable();
			$('restockButton').hide();
			$('li_restock_when').show();
			$('li_restock_time').show();
			
			$('restock_when_ques').innerHTML = ipb.lang['form_stock_reaches'];
			$('restock_when_desc').innerHTML = ipb.lang['form_stock_reaches_desc'];
			$('restock_time_ques').innerHTML = ipb.lang['form_restock_time_1'];
			$('restock_time_desc').innerHTML = "";
			
			var whenStock = parseInt($F($('item[restock_when]')));
			if(!isNaN(whenStock) && whenStock >= 0)
			{
				var time = $A();
				
				if($F($('item[restock_day]')) == 1)
				{
					time[time.size()] = ipb.lang['1_day'];
				}
				else if($F($('item[restock_day]')) > 1)
				{
					time[time.size()] = ipb.lang['x_days'].sub("%s", $F($('item[restock_day]')));
				}
				if($F($('item[restock_hour]')) == 1)
				{
					time[time.size()] = ipb.lang['1_hour'];
				}
				else if($F($('item[restock_hour]')) > 1)
				{
					time[time.size()] = ipb.lang['x_hours'].sub("%s", $F($('item[restock_hour]')));
				}
				if($F($('item[restock_minute]')) == 1)
				{
					time[time.size()] = ipb.lang['1_minute'];
				}
				else if($F($('item[restock_minute]')) > 1)
				{
					time[time.size()] = ipb.lang['x_minutes'].sub("%s", $F($('item[restock_minute]')));
				}
				if(time.size() == 0)
				{
					time[time.size()] = ipb.lang['instantly'];
				}
				
				var message = ipb.lang['restock_message_1'];
				message = message.sub("<#AMOUNT#>",howMuch);
				message = message.sub("<#TIME#>",time.join(", "));
				message = message.sub("<#STOCK#>",whenStock);
				
				$('restockInfo').value = message;
				
				return;
			}
		}
		else if($F($('item[restock]')) == 2)
		{
			$('item[restock_when]').enable();
			$('item[restock_minute]').enable();
			$('item[restock_hour]').enable();
			$('item[restock_day]').enable();
			$('restockButton').disable();
			$('restockButton').hide();
			$('li_restock_when').show();
			$('li_restock_time').show();
			
			$('restock_when_ques').innerHTML = ipb.lang['form_stock_reaches'];
			$('restock_when_desc').innerHTML = ipb.lang['form_stock_reaches_desc'];
			$('restock_time_ques').innerHTML = ipb.lang['form_restock_time_1'];
			$('restock_time_desc').innerHTML = "";
			
			if($F($('item[restock_hour]')) == 24)
			{
				$('item[restock_day]').value = 7;
			}
			
			var whenStock = parseInt($F($('item[restock_when]')));
			if(!isNaN(whenStock) && whenStock >= 0)
			{
				var time;
				
				if($F($('item[restock_hour]')) >= 24)
				{
					time = ipb.lang['any_day_any_hour_x_minute'];
					time = time.sub("%s",$F($('item[restock_minute]')));
				}
				else//if($F($('item[restock_hour]')) < 24)
				{
					var timeOfDay;
					if($F($('item[restock_minute]')) < 10)
					{
						timeOfDay = "0"+$F($('item[restock_minute]'));
					}
					else
					{
						timeOfDay = $F($('item[restock_minute]'));
					}
					
					if($F($('item[restock_hour]')) == 0)
					{
						timeOfDay = 12 + ":" + timeOfDay + "AM";
					}
					else if($F($('item[restock_hour]')) < 12)
					{
						timeOfDay = $F($('item[restock_hour]')) + ":" + timeOfDay + "AM";
					}
					else if($F($('item[restock_hour]')) == 12)
					{
						timeOfDay = $F($('item[restock_hour]')) + ":" + timeOfDay + "PM";
					}
					else//if($F($('item[restock_hour]')) < 23)
					{
						timeOfDay = ($F($('item[restock_hour]'))-12) + ":" + timeOfDay + "PM";
					}
					
					if($F($('item[restock_day]')) >= 7)
					{
						time = ipb.lang['any_day_at_time'];
						time = time.sub("%s",timeOfDay);
					}
					else//if($F($('item[restock_day]')) < 7)
					{
						time = ipb.lang['any_x_at_time'];
						time = time.sub("<#TIME#>",timeOfDay);
						time = time.sub("<#DAY#>",ipb.lang['dayweek'+$F($('item[restock_day]'))]);
					}
				}
				
				var message = ipb.lang['restock_message_2'];
				message = message.sub("<#AMOUNT#>",howMuch);
				message = message.sub("<#TIME#>",time);
				message = message.sub("<#STOCK#>",whenStock);
				
				$('restockInfo').value = message;
				
				return;
			}
		}
		else if($F($('item[restock]')) == 3)
		{
			$('item[restock_when]').enable();
			$('item[restock_minute]').enable();
			$('item[restock_hour]').enable();
			$('item[restock_day]').enable();
			$('restockButton').enable();
			$('restockButton').show();
			$('li_restock_when').show();
			$('li_restock_time').show();
			
			$('restock_when_ques').innerHTML = ipb.lang['form_last_stock'];
			$('restock_when_desc').innerHTML = ipb.lang['form_last_stock_desc'];
			$('restock_time_ques').innerHTML = ipb.lang['form_restock_time_2'];
			$('restock_time_desc').innerHTML = "";
			
			if(!$F($('item[restock_when]')))
			{
				$('restockInfo').value = ipb.lang['bad_time_format'];
				return;
			}
			
			var interval = $F($('item[restock_day]')) * 86400 + $F($('item[restock_hour]')) * 3600 + $F($('item[restock_minute]')) * 60;
			
			if(interval > 0)
			{
				var dateTime = $F($('item[restock_when]')).split(" ");
				if(dateTime.length != 2)
				{
					$('restockInfo').value = ipb.lang['bad_time_format'];
					return;
				}
				var dateArray = dateTime[0].split("/"); 
				if(dateArray.length != 3)
				{
					$('restockInfo').value = ipb.lang['bad_time_format'];
					return;
				}
				dateArray[0] = parseInt(dateArray[0].replace(/^0+/,''));
				dateArray[1] = parseInt(dateArray[1].replace(/^0+/,''));
				dateArray[2] = parseInt(dateArray[2].replace(/^0+/,''));
				if(isNaN(dateArray[0]) || isNaN(dateArray[1]) || isNaN(dateArray[2]))
				{
					$('restockInfo').value = ipb.lang['bad_time_format'];
					return;
				}
				var timeArray = dateTime[1].split(":");
				if(timeArray.length != 2)
				{
					$('restockInfo').value = ipb.lang['bad_time_format'];
					return;
				}
				timeArray[0] = parseInt(timeArray[0],10);
				timeArray[1] = parseInt(timeArray[1],10);
				if(isNaN(timeArray[0]) || isNaN(timeArray[1]))
				{
					$('restockInfo').value = ipb.lang['bad_time_format'];
					return;
				}
				
				var time = $A();
				if($F($('item[restock_day]')) == 1)
				{
					time[time.size()] = ipb.lang['1_day'];
				}
				else if($F($('item[restock_day]')) > 1)
				{
					time[time.size()] = ipb.lang['x_days'].sub("%s", $F($('item[restock_day]')));
				}
				if($F($('item[restock_hour]')) == 1)
				{
					time[time.size()] = ipb.lang['1_hour'];
				}
				else if($F($('item[restock_hour]')) > 1)
				{
					time[time.size()] = ipb.lang['x_hours'].sub("%s", $F($('item[restock_hour]')));
				}
				if($F($('item[restock_minute]')) == 1)
				{
					time[time.size()] = ipb.lang['1_minute'];
				}
				else if($F($('item[restock_minute]')) > 1)
				{
					time[time.size()] = ipb.lang['x_minutes'].sub("%s", $F($('item[restock_minute]')));
				}
				
				var currentDate = new Date();
				currentDate.setSeconds(0,0);
				var current = currentDate.getTime();
				var nextDate = new Date();
				nextDate.setUTCFullYear(dateArray[2],dateArray[0]-1,dateArray[1]);
				nextDate.setUTCHours(timeArray[0],timeArray[1],0,0);
				var nextTime = nextDate.getTime();
				var offset = nextTime % (interval * 1000); // in ms
				var intervals = Math.floor(current / (interval * 1000)) + 1;
				
				nextDate.setTime(intervals * interval * 1000 + offset);
				
				var minute = nextDate.getUTCMinutes();
				if(minute < 10)
				{
					minute = "0"+minute;
				}
				var when = (nextDate.getUTCMonth()+1)+"/"+nextDate.getUTCDate()+"/"+nextDate.getUTCFullYear()+" "+nextDate.getUTCHours()+":"+minute;
				
				var message = ipb.lang['restock_message_3'];
				message = message.sub("<#AMOUNT#>",howMuch);
				message = message.sub("<#TIME#>",time.join(", "));
				message = message.sub("<#WHEN#>",when);
				
				$('restockInfo').value = message;
				
				return;
			}
			else
			{
				var message = ipb.lang['restock_message_3_second'];
				message = message.sub("<#AMOUNT#>",howMuch);
				
				$('restockInfo').value = message;
				
				return;
			}
		}
		else if($F($('item[restock]')) == 4)
		{
			$('item[restock_when]').disable();
			$('item[restock_minute]').enable();
			$('item[restock_hour]').enable();
			$('item[restock_day]').enable();
			$('restockButton').disable();
			$('restockButton').hide();
			$('li_restock_when').hide();
			$('li_restock_time').show();
			
			$('restock_when_ques').innerHTML = "";
			$('restock_when_desc').innerHTML = "";
			$('restock_time_ques').innerHTML = ipb.lang['form_restock_time_3'];
			$('restock_time_desc').innerHTML = ipb.lang['form_restock_time_desc'];
			
			if($F($('item[restock_hour]')) == 24)
			{
				$('item[restock_day]').value = 7;
			}
			
			var time;
			
			if($F($('item[restock_hour]')) >= 24)
			{
				time = ipb.lang['every_day_every_hour_x_minute'];
				time = time.sub("%s",$F($('item[restock_minute]')));
			}
			else//if($F($('item[restock_hour]')) < 24)
			{
				var timeOfDay;
				if($F($('item[restock_minute]')) < 10)
				{
					timeOfDay = "0"+$F($('item[restock_minute]'));
				}
				else
				{
					timeOfDay = $F($('item[restock_minute]'));
				}
				
				if($F($('item[restock_hour]')) == 0)
				{
					timeOfDay = 12 + ":" + timeOfDay + "AM";
				}
				else if($F($('item[restock_hour]')) < 12)
				{
					timeOfDay = $F($('item[restock_hour]')) + ":" + timeOfDay + "AM";
				}
				else if($F($('item[restock_hour]')) == 12)
				{
					timeOfDay = $F($('item[restock_hour]')) + ":" + timeOfDay + "PM";
				}
				else//if($F($('item[restock_hour]')) < 23)
				{
					timeOfDay = ($F($('item[restock_hour]'))-12) + ":" + timeOfDay + "PM";
				}
				
				if($F($('item[restock_day]')) >= 7)
				{
					time = ipb.lang['every_day_at_time'];
					time = time.sub("%s",timeOfDay);
				}
				else//if($F($('item[restock_day]')) < 7)
				{
					time = ipb.lang['every_x_at_time'];
					time = time.sub("<#TIME#>",timeOfDay);
					time = time.sub("<#DAY#>",ipb.lang['dayweek'+$F($('item[restock_day]'))]);
				}
			}
			
			var message = ipb.lang['restock_message_4'];
			message = message.sub("<#AMOUNT#>",howMuch);
			message = message.sub("<#TIME#>",time);
			
			$('restockInfo').value = message;
			
			return;
		}
		
		$('restockInfo').value = ipb.lang['restock_bad_input'];
	},
	setTime: function(event)
	{
		var date = new Date();
		var minute = date.getMinutes();
		if(minute < 10)
		{
			minute = "0"+minute;
		}
		
		$('item[restock_when]').value = (date.getUTCMonth()+1)+"/"+date.getUTCDate()+"/"+date.getUTCFullYear()+" "+date.getUTCHours()+":"+date.getUTCMinutes();
		
		ibMarketItemForm.updateResotck();
		
		$('item[restock_when]').focus();
	}
}

ibMarketItemForm.init();
